home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / coffread.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  59KB  |  2,138 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
  3.    Copyright (C) 1987-1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "symtab.h"
  25. #include "breakpoint.h"
  26. #include "bfd.h"
  27. #include "symfile.h"
  28.  
  29. #if defined (TDESC)
  30. /* Need to get C_VERSION and friends.  FIXME, should be in internalcoff.h */
  31. #include "m88k-bcs.h"
  32. #endif /* not TDESC */
  33.  
  34. #include <obstack.h>
  35. #include <string.h>
  36.  
  37. #include "internalcoff.h"    /* Internal format of COFF symbols in BFD */
  38. #include "libcoff.h"        /* FIXME secret internal data from BFD */
  39.  
  40. static void add_symbol_to_list ();
  41. static void read_coff_symtab ();
  42. static void patch_opaque_types ();
  43. static struct type *decode_function_type ();
  44. static struct type *decode_type ();
  45. static struct type *decode_base_type ();
  46. static struct type *read_enum_type ();
  47. static struct type *read_struct_type ();
  48. static void finish_block ();
  49. static struct blockvector *make_blockvector ();
  50. static struct symbol *process_coff_symbol ();
  51. static int init_stringtab ();
  52. static void free_stringtab ();
  53. static char *getfilename ();
  54. static char *getsymname ();
  55. static int init_lineno ();
  56. static void enter_linenos ();
  57. static void read_one_sym ();
  58.  
  59. extern int fclose ();
  60. extern void free_all_symtabs ();
  61. extern void free_all_psymtabs ();
  62.  
  63. /* To be an sdb debug type, type must have at least a basic or primary
  64.    derived type.  Using this rather than checking against T_NULL is
  65.    said to prevent core dumps if we try to operate on Michael Bloom
  66.    dbx-in-coff file.  */
  67.  
  68. #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
  69.  
  70. /*
  71.  * Convert from an sdb register number to an internal gdb register number.
  72.  * This should be defined in tm.h, if REGISTER_NAMES is not set up
  73.  * to map one to one onto the sdb register numbers.
  74.  */
  75. #ifndef SDB_REG_TO_REGNUM
  76. # define SDB_REG_TO_REGNUM(value)     (value)
  77. #endif
  78.  
  79. /* Name of source file whose symbol data we are now processing.
  80.    This comes from a symbol named ".file".  */
  81.  
  82. static char *last_source_file;
  83.  
  84. /* Core address of start and end of text of current source file.
  85.    This comes from a ".text" symbol where x_nlinno > 0.  */
  86.  
  87. static CORE_ADDR cur_src_start_addr;
  88. static CORE_ADDR cur_src_end_addr;
  89.  
  90. /* Core address of the end of the first object file.  */
  91. static CORE_ADDR first_object_file_end;
  92.  
  93. /* The addresses of the symbol table stream and number of symbols
  94.    of the object file we are reading (as copied into core).  */
  95.  
  96. static FILE *nlist_stream_global;
  97. static int nlist_nsyms_global;
  98.  
  99. /* The entry point (starting address) of the file, if it is an executable.  */
  100.  
  101. static CORE_ADDR entry_point;
  102.  
  103. /* The index in the symbol table of the last coff symbol that was processed.  */
  104.  
  105. static int symnum;
  106.  
  107. /* Vector of types defined so far, indexed by their coff symnum.  */
  108.  
  109. static struct type **type_vector;
  110.  
  111. /* Number of elements allocated for type_vector currently.  */
  112.  
  113. static int type_vector_length;
  114.  
  115. /* Vector of line number information.  */
  116.  
  117. static struct linetable *line_vector;
  118.  
  119. /* Index of next entry to go in line_vector_index.  */
  120.  
  121. static int line_vector_index;
  122.  
  123. /* Last line number recorded in the line vector.  */
  124.  
  125. static int prev_line_number;
  126.  
  127. /* Number of elements allocated for line_vector currently.  */
  128.  
  129. static int line_vector_length;
  130.  
  131. /* Pointers to scratch storage, used for reading raw symbols and auxents.  */
  132.  
  133. static char *temp_sym;
  134. static char *temp_aux;
  135.  
  136. /* Local variables that hold the shift and mask values for the
  137.    COFF file that we are currently reading.  These come back to us
  138.    from BFD, and are referenced by their macro names, as well as
  139.    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
  140.    macros from ../internalcoff.h .  */
  141.  
  142. static unsigned    local_n_btmask;
  143. static unsigned    local_n_btshft;
  144. static unsigned    local_n_tmask;
  145. static unsigned    local_n_tshift;
  146.  
  147. #define    N_BTMASK    local_n_btmask
  148. #define    N_BTSHFT    local_n_btshft
  149. #define    N_TMASK        local_n_tmask
  150. #define    N_TSHIFT    local_n_tshift
  151.  
  152. /* Local variables that hold the sizes in the file of various COFF structures.
  153.    (We only need to know this to read them from the file -- BFD will then
  154.    translate the data in them, into `internal_xxx' structs in the right
  155.    byte order, alignment, etc.)  */
  156.  
  157. static unsigned    local_linesz;
  158. static unsigned    local_symesz;
  159. static unsigned    local_auxesz;
  160.  
  161.  
  162. #ifdef TDESC
  163. #include "tdesc.h"
  164. #define SEM
  165. int int_sem_val = 's' << 24 | 'e' << 16 | 'm' << 8 | '.';
  166. int temp_sem_val;
  167. int last_coffsem = 2;
  168. #if 0
  169.   /* This isn't used currently.  */
  170. int last_coffsyn = 0;
  171. #endif
  172. int debug_info = 0;    /*used by tdesc */
  173. extern dc_dcontext_t tdesc_handle;
  174. extern int safe_to_init_tdesc_context;
  175. #endif
  176.  
  177. /* Chain of typedefs of pointers to empty struct/union types.
  178.    They are chained thru the SYMBOL_VALUE_CHAIN.  */
  179.  
  180. #define HASHSIZE 127
  181. static struct symbol *opaque_type_chain[HASHSIZE];
  182.  
  183. /* Record the symbols defined for each context in a list.
  184.    We don't create a struct block for the context until we
  185.    know how long to make it.  */
  186.  
  187. struct pending
  188. {
  189.   struct pending *next;
  190.   struct symbol *symbol;
  191. };
  192.  
  193. /* Here are the three lists that symbols are put on.  */
  194.  
  195. struct pending *file_symbols;    /* static at top level, and types */
  196.  
  197. struct pending *global_symbols;    /* global functions and variables */
  198.  
  199. struct pending *local_symbols;    /* everything local to lexical context */
  200.  
  201. /* List of unclosed lexical contexts
  202.    (that will become blocks, eventually).  */
  203.  
  204. struct context_stack
  205. {
  206.   struct context_stack *next;
  207.   struct pending *locals;
  208.   struct pending_block *old_blocks;
  209.   struct symbol *name;
  210.   CORE_ADDR start_addr;
  211.   int depth;
  212. };
  213.  
  214. struct context_stack *context_stack;
  215.  
  216. /* Nonzero if within a function (so symbols should be local,
  217.    if nothing says specifically).  */
  218.  
  219. int within_function;
  220.  
  221. #if 0
  222. /* The type of the function we are currently reading in.  This is
  223.    used by define_symbol to record the type of arguments to a function. */
  224.  
  225. struct type *in_function_type;
  226. #endif
  227.  
  228. /* List of blocks already made (lexical contexts already closed).
  229.    This is used at the end to make the blockvector.  */
  230.  
  231. struct pending_block
  232. {
  233.   struct pending_block *next;
  234.   struct block *block;
  235. };
  236.  
  237. struct pending_block *pending_blocks;
  238.  
  239. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  240. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  241.  
  242. /* Complaints about various problems in the file being read  */
  243.  
  244. struct complaint ef_complaint = 
  245.   {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
  246.  
  247. struct complaint lineno_complaint =
  248.   {"Line number pointer %d lower than start of line numbers", 0, 0};
  249.  
  250.  
  251. /* Look up a coff type-number index.  Return the address of the slot
  252.    where the type for that index is stored.
  253.    The type-number is in INDEX. 
  254.  
  255.    This can be used for finding the type associated with that index
  256.    or for associating a new type with the index.  */
  257.  
  258. static struct type **
  259. coff_lookup_type (index)
  260.      register int index;
  261. {
  262.   if (index >= type_vector_length)
  263.     {
  264.       int old_vector_length = type_vector_length;
  265.  
  266.       type_vector_length *= 2;
  267.       if (type_vector_length < index) {
  268.     type_vector_length = index * 2;
  269.       }
  270.       type_vector = (struct type **)
  271.     xrealloc (type_vector, type_vector_length * sizeof (struct type *));
  272.       bzero (&type_vector[old_vector_length],
  273.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  274.     }
  275.   return &type_vector[index];
  276. }
  277.  
  278. /* Make sure there is a type allocated for type number index
  279.    and return the type object.
  280.    This can create an empty (zeroed) type object.  */
  281.  
  282. static struct type *
  283. coff_alloc_type (index)
  284.      int index;
  285. {
  286.   register struct type **type_addr = coff_lookup_type (index);
  287.   register struct type *type = *type_addr;
  288.  
  289.   /* If we are referring to a type not known at all yet,
  290.      allocate an empty type for it.
  291.      We will fill it in later if we find out how.  */
  292.   if (type == 0)
  293.     {
  294.       type = (struct type *) obstack_alloc (symbol_obstack,
  295.                         sizeof (struct type));
  296.       bzero (type, sizeof (struct type));
  297.       TYPE_VPTR_FIELDNO (type) = -1;
  298.       *type_addr = type;
  299.     }
  300.   return type;
  301. }
  302.  
  303. /* maintain the lists of symbols and blocks */
  304.  
  305. /* Add a symbol to one of the lists of symbols.  */
  306. static void
  307. add_symbol_to_list (symbol, listhead)
  308.      struct symbol *symbol;
  309.      struct pending **listhead;
  310. {
  311.   register struct pending *link
  312.     = (struct pending *) xmalloc (sizeof (struct pending));
  313.  
  314.   link->next = *listhead;
  315.   link->symbol = symbol;
  316.   *listhead = link;
  317. }
  318.  
  319. /* Take one of the lists of symbols and make a block from it.
  320.    Put the block on the list of pending blocks.  */
  321.  
  322. static void
  323. finish_block (symbol, listhead, old_blocks, start, end)
  324.      struct symbol *symbol;
  325.      struct pending **listhead;
  326.      struct pending_block *old_blocks;
  327.      CORE_ADDR start, end;
  328. {
  329.   register struct pending *next, *next1;
  330.   register struct block *block;
  331.   register struct pending_block *pblock;
  332.   struct pending_block *opblock;
  333.   register int i;
  334.  
  335.   /* Count the length of the list of symbols.  */
  336.  
  337.   for (next = *listhead, i = 0; next; next = next->next, i++);
  338.  
  339.   block = (struct block *)
  340.         obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  341.  
  342.   /* Copy the symbols into the block.  */
  343.  
  344.   BLOCK_NSYMS (block) = i;
  345.   for (next = *listhead; next; next = next->next)
  346.     BLOCK_SYM (block, --i) = next->symbol;
  347.  
  348.   BLOCK_START (block) = start;
  349.   BLOCK_END (block) = end;
  350.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  351.  
  352.   /* Put the block in as the value of the symbol that names it.  */
  353.  
  354.   if (symbol)
  355.     {
  356.       SYMBOL_BLOCK_VALUE (symbol) = block;
  357.       BLOCK_FUNCTION (block) = symbol;
  358.     }
  359.   else
  360.     BLOCK_FUNCTION (block) = 0;
  361.  
  362.   /* Now free the links of the list, and empty the list.  */
  363.  
  364.   for (next = *listhead; next; next = next1)
  365.     {
  366.       next1 = next->next;
  367.       free (next);
  368.     }
  369.   *listhead = 0;
  370.  
  371.   /* Install this block as the superblock
  372.      of all blocks made since the start of this scope
  373.      that don't have superblocks yet.  */
  374.  
  375.   opblock = 0;
  376.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  377.     {
  378.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  379.     BLOCK_SUPERBLOCK (pblock->block) = block;
  380.       opblock = pblock;
  381.     }
  382.  
  383.   /* Record this block on the list of all blocks in the file.
  384.      Put it after opblock, or at the beginning if opblock is 0.
  385.      This puts the block in the list after all its subblocks.  */
  386.  
  387.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  388.   pblock->block = block;
  389.   if (opblock)
  390.     {
  391.       pblock->next = opblock->next;
  392.       opblock->next = pblock;
  393.     }
  394.   else
  395.     {
  396.       pblock->next = pending_blocks;
  397.       pending_blocks = pblock;
  398.     }
  399. }
  400.  
  401. static struct blockvector *
  402. make_blockvector ()
  403. {
  404.   register struct pending_block *next, *next1;
  405.   register struct blockvector *blockvector;
  406.   register int i;
  407.  
  408.   /* Count the length of the list of blocks.  */
  409.  
  410.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  411.  
  412.   blockvector = (struct blockvector *)
  413.           obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  414.  
  415.   /* Copy the blocks into the blockvector.
  416.      This is done in reverse order, which happens to put
  417.      the blocks into the proper order (ascending starting address).
  418.      finish_block has hair to insert each block into the list
  419.      after its subblocks in order to make sure this is true.  */
  420.  
  421.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  422.   for (next = pending_blocks; next; next = next->next)
  423.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  424.  
  425.   /* Now free the links of the list, and empty the list.  */
  426.  
  427.   for (next = pending_blocks; next; next = next1)
  428.     {
  429.       next1 = next->next;
  430.       free (next);
  431.     }
  432.   pending_blocks = 0;
  433.  
  434.   return blockvector;
  435. }
  436.  
  437. /* Manage the vector of line numbers.  */
  438.  
  439. static void
  440. record_line (line, pc)
  441.      int line;
  442.      CORE_ADDR pc;
  443. {
  444.   struct linetable_entry *e;
  445.   /* Make sure line vector is big enough.  */
  446.  
  447.   if (line_vector_index + 2 >= line_vector_length)
  448.     {
  449.       line_vector_length *= 2;
  450.       line_vector = (struct linetable *)
  451.     xrealloc (line_vector, sizeof (struct linetable)
  452.           + (line_vector_length
  453.              * sizeof (struct linetable_entry)));
  454.     }
  455.  
  456.   e = line_vector->item + line_vector_index++;
  457.   e->line = line; e->pc = pc;
  458. }
  459.  
  460. /* Start a new symtab for a new source file.
  461.    This is called when a COFF ".file" symbol is seen;
  462.    it indicates the start of data for one original source file.  */
  463.  
  464. static void
  465. start_symtab ()
  466. {
  467.   file_symbols = 0;
  468.   global_symbols = 0;
  469.   context_stack = 0;
  470.   within_function = 0;
  471.   last_source_file = 0;
  472. #ifdef TDESC
  473.   last_coffsem = 2;
  474. #if 0
  475.   /* This isn't used currently.  */
  476.   last_coffsyn = 0;
  477. #endif
  478. #endif
  479.  
  480.   /* Initialize the source file line number information for this file.  */
  481.  
  482.   if (line_vector)        /* Unlikely, but maybe possible? */
  483.     free (line_vector);
  484.   line_vector_index = 0;
  485.   line_vector_length = 1000;
  486.   prev_line_number = -2;    /* Force first line number to be explicit */
  487.   line_vector = (struct linetable *)
  488.     xmalloc (sizeof (struct linetable)
  489.          + line_vector_length * sizeof (struct linetable_entry));
  490. }
  491.  
  492. /* Save the vital information from when starting to read a file,
  493.    for use when closing off the current file.
  494.    NAME is the file name the symbols came from, START_ADDR is the first
  495.    text address for the file, and SIZE is the number of bytes of text.  */
  496.  
  497. static void
  498. complete_symtab (name, start_addr, size)
  499.     char *name;
  500.     CORE_ADDR start_addr;
  501.     unsigned int size;
  502. {
  503.   last_source_file = savestring (name, strlen (name));
  504.   cur_src_start_addr = start_addr;
  505.   cur_src_end_addr = start_addr + size;
  506.  
  507.   if (entry_point < cur_src_end_addr
  508.       && entry_point >= cur_src_start_addr)
  509.     {
  510.       startup_file_start = cur_src_start_addr;
  511.       startup_file_end = cur_src_end_addr;
  512.     }
  513. }
  514.  
  515. /* Finish the symbol definitions for one main source file,
  516.    close off all the lexical contexts for that file
  517.    (creating struct block's for them), then make the
  518.    struct symtab for that file and put it in the list of all such. */
  519.  
  520. static void
  521. end_symtab ()
  522. {
  523.   register struct symtab *symtab;
  524.   register struct context_stack *cstk;
  525.   register struct blockvector *blockvector;
  526.   register struct linetable *lv;
  527.  
  528.   /* Finish the lexical context of the last function in the file.  */
  529.  
  530.   if (context_stack)
  531.     {
  532.       cstk = context_stack;
  533.       context_stack = 0;
  534.       /* Make a block for the local symbols within.  */
  535.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  536.             cstk->start_addr, cur_src_end_addr);
  537.       free (cstk);
  538.     }
  539.  
  540.   /* Ignore a file that has no functions with real debugging info.  */
  541.   if (pending_blocks == 0 && file_symbols == 0 && global_symbols == 0)
  542.     {
  543.       free (line_vector);
  544.       line_vector = 0;
  545.       line_vector_length = -1;
  546.       last_source_file = 0;
  547.       return;
  548.     }
  549.  
  550.   /* Create the two top-level blocks for this file (STATIC_BLOCK and
  551.      GLOBAL_BLOCK).  */
  552.   finish_block (0, &file_symbols, 0, cur_src_start_addr, cur_src_end_addr);
  553.   finish_block (0, &global_symbols, 0, cur_src_start_addr, cur_src_end_addr);
  554.  
  555.   /* Create the blockvector that points to all the file's blocks.  */
  556.   blockvector = make_blockvector ();
  557.  
  558.   /* Now create the symtab object for this source file.  */
  559.   symtab = allocate_symtab (last_source_file);
  560.  
  561.   /* Fill in its components.  */
  562.   symtab->blockvector = blockvector;
  563.   symtab->free_code = free_linetable;
  564.   symtab->free_ptr = 0;
  565.   symtab->filename = last_source_file;
  566.   symtab->dirname = NULL;
  567.   lv = line_vector;
  568.   lv->nitems = line_vector_index;
  569.   symtab->linetable = (struct linetable *)
  570.     xrealloc (lv, (sizeof (struct linetable)
  571.            + lv->nitems * sizeof (struct linetable_entry)));
  572.  
  573. #ifdef TDESC
  574.   symtab->coffsem = last_coffsem;
  575. #if 0
  576.   /* This isn't used currently.  Besides, if this is really about "syntax",
  577.      it shouldn't need to stick around past symbol read-in time.  */
  578.   symtab->coffsyn = last_coffsyn;
  579. #endif
  580. #endif
  581.  
  582.   free_named_symtabs (symtab->filename);
  583.  
  584.   /* Link the new symtab into the list of such.  */
  585.   symtab->next = symtab_list;
  586.   symtab_list = symtab;
  587.  
  588.   /* Reinitialize for beginning of new file. */
  589.   line_vector = 0;
  590.   line_vector_length = -1;
  591.   last_source_file = 0;
  592. }
  593.  
  594. static void
  595. record_misc_function (name, address)
  596.      char *name;
  597.      CORE_ADDR address;
  598. {
  599. #ifdef TDESC
  600.   /* We don't want TDESC entry points on the misc_function_vector */
  601.   if (name[0] == '@') return;
  602. #endif
  603.   /* mf_text isn't true, but apparently COFF doesn't tell us what it really
  604.      is, so this guess is more useful than mf_unknown.  */
  605.   prim_record_misc_function (savestring (name, strlen (name)),
  606.                  address,
  607.                  (int)mf_text);
  608. }
  609.  
  610. /* coff_symfile_init ()
  611.    is the coff-specific initialization routine for reading symbols.
  612.    It is passed a struct sym_fns which contains, among other things,
  613.    the BFD for the file whose symbols are being read, and a slot for
  614.    a pointer to "private data" which we fill with cookies and other
  615.    treats for coff_symfile_read ().
  616.  
  617.    We will only be called if this is a COFF or COFF-like file.
  618.    BFD handles figuring out the format of the file, and code in symtab.c
  619.    uses BFD's determination to vector to us.
  620.  
  621.    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
  622.  
  623. struct coff_symfile_info {
  624.   file_ptr min_lineno_offset;        /* Where in file lowest line#s are */
  625.   file_ptr max_lineno_offset;        /* 1+last byte of line#s in file */
  626. };
  627.  
  628. static int text_bfd_scnum;
  629.  
  630. static void
  631. coff_symfile_init (sf)
  632.      struct sym_fns *sf;
  633. {
  634.   asection    *section;
  635.   bfd *abfd = sf->sym_bfd;
  636.  
  637.   /* Allocate struct to keep track of the symfile */
  638.   /* FIXME memory leak */
  639.   sf->sym_private = xmalloc (sizeof (struct coff_symfile_info));
  640.  
  641. #if defined (TDESC)
  642.   safe_to_init_tdesc_context  = 0;
  643. #endif
  644.  
  645.   /* Save startup file's range of PC addresses to help blockframe.c
  646.      decide where the bottom of the stack is.  */
  647.   if (bfd_get_file_flags (abfd) & EXEC_P)
  648.     {
  649.       /* Executable file -- record its entry point so we'll recognize
  650.      the startup file because it contains the entry point.  */
  651.       entry_point = bfd_get_start_address (abfd);
  652.     }
  653.   else
  654.     {
  655.       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
  656.       /* ~0 will not be in any file, we hope.  */
  657.       entry_point = ~0;
  658.       /* set the startup file to be an empty range.  */
  659.       startup_file_start = 0;
  660.       startup_file_end = 0;
  661.     }
  662.    /* Save the section number for the text section */
  663.    if (section = bfd_get_section_by_name(abfd,".text"))
  664.        text_bfd_scnum = section->index;
  665.    else
  666.        text_bfd_scnum = -1; 
  667. }
  668.  
  669. /* This function is called for every section; it finds the outer limits
  670.    of the line table (minimum and maximum file offset) so that the
  671.    mainline code can read the whole thing for efficiency.  */
  672.  
  673. /* ARGSUSED */
  674. static void
  675. find_linenos (abfd, asect, vpinfo)
  676.      bfd *abfd;
  677.      sec_ptr asect;
  678.      void *vpinfo;
  679. {
  680.   struct coff_symfile_info *info;
  681.   int size, count;
  682.   file_ptr offset, maxoff;
  683.  
  684. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  685.   count = asect->lineno_count;
  686. /* End of warning */
  687.  
  688.   if (count == 0)
  689.     return;
  690.   size = count * local_linesz;
  691.  
  692.   info = (struct coff_symfile_info *)vpinfo;
  693. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  694.   offset = asect->line_filepos;
  695. /* End of warning */
  696.  
  697.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  698.     info->min_lineno_offset = offset;
  699.  
  700.   maxoff = offset + size;
  701.   if (maxoff > info->max_lineno_offset)
  702.     info->max_lineno_offset = maxoff;
  703. #ifdef TDESC
  704.   /* While we're at it, find the debug_info.  It's in the s_relptr
  705.      (or, in BFD-speak, rel_filepos) of the text segment section header.  */
  706.   if (strcmp (bfd_section_name (abfd, asect), ".text") == 0)
  707.     {
  708.       /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  709.       debug_info = asect->rel_filepos;
  710.       /* End of warning */
  711.       if (tdesc_handle)
  712.     {
  713.       dc_terminate (tdesc_handle);
  714.       tdesc_handle = 0;
  715.     }
  716.     }
  717. #endif /* TDESC */
  718. }
  719.  
  720.  
  721. /* The BFD for this file -- only good while we're actively reading
  722.    symbols into a psymtab or a symtab.  */
  723.  
  724. static bfd *symfile_bfd;
  725.  
  726. /* Read a symbol file, after initialization by coff_symfile_init.  */
  727. /* FIXME!  Addr and Mainline are not used yet -- this will not work for
  728.    shared libraries or add_file!  */
  729.  
  730. /* ARGSUSED */
  731. static void
  732. coff_symfile_read (sf, addr, mainline)
  733.      struct sym_fns *sf;
  734.      CORE_ADDR addr;
  735.      int mainline;
  736. {
  737.   struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
  738.   bfd *abfd = sf->sym_bfd;
  739.   coff_data_type *cdata = coff_data (abfd);
  740.   char *name = bfd_get_filename (abfd);
  741.   int desc;
  742.   register int val;
  743.   int num_symbols;
  744.   int symtab_offset;
  745.   int stringtab_offset;
  746.  
  747.   symfile_bfd = abfd;            /* Kludge for swap routines */
  748.  
  749. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  750.    desc = fileno ((FILE *)(abfd->iostream));    /* File descriptor */
  751.    num_symbols = bfd_get_symcount (abfd);    /* How many syms */
  752.    symtab_offset = cdata->sym_filepos;        /* Symbol table file offset */
  753.    stringtab_offset = symtab_offset +        /* String table file offset */
  754.               num_symbols * cdata->local_symesz;
  755.  
  756.   /* Set a few file-statics that give us specific information about
  757.      the particular COFF file format we're reading.  */
  758.   local_linesz   = cdata->local_linesz;
  759.   local_n_btmask = cdata->local_n_btmask;
  760.   local_n_btshft = cdata->local_n_btshft;
  761.   local_n_tmask  = cdata->local_n_tmask;
  762.   local_n_tshift = cdata->local_n_tshift;
  763.   local_linesz   = cdata->local_linesz;
  764.   local_symesz   = cdata->local_symesz;
  765.   local_auxesz   = cdata->local_auxesz;
  766.  
  767.   /* Allocate space for raw symbol and aux entries, based on their
  768.      space requirements as reported by BFD.  */
  769.   temp_sym = (char *) xmalloc
  770.      (cdata->local_symesz + cdata->local_auxesz);
  771.   temp_aux = temp_sym + cdata->local_symesz;
  772.   make_cleanup (free_current_contents, &temp_sym);
  773. /* End of warning */
  774.  
  775.   /* Read the line number table, all at once.  */
  776.   info->min_lineno_offset = 0;
  777.   info->max_lineno_offset = 0;
  778.   bfd_map_over_sections (abfd, find_linenos, info);
  779.  
  780.   val = init_lineno (desc, info->min_lineno_offset, 
  781.              info->max_lineno_offset - info->min_lineno_offset);
  782.   if (val < 0)
  783.     error ("\"%s\": error reading line numbers\n", name);
  784.  
  785.   /* Now read the string table, all at once.  */
  786.  
  787.   val = init_stringtab (desc, stringtab_offset);
  788.   if (val < 0)
  789.     {
  790.       free_all_symtabs ();        /* FIXME blows whole symtab */
  791.       printf ("\"%s\": can't get string table", name);
  792.       fflush (stdout);
  793.       return;
  794.     }
  795.   make_cleanup (free_stringtab, 0);
  796.  
  797.   /* Position to read the symbol table.  Do not read it all at once. */
  798.   val = lseek (desc, (long)symtab_offset, 0);
  799.   if (val < 0)
  800.     perror_with_name (name);
  801.  
  802.   init_misc_bunches ();
  803.   make_cleanup (discard_misc_bunches, 0);
  804.  
  805.   /* Now that the executable file is positioned at symbol table,
  806.      process it and define symbols accordingly.  */
  807.  
  808.   read_coff_symtab (desc, num_symbols);
  809.  
  810.   patch_opaque_types ();
  811.  
  812.   /* Sort symbols alphabetically within each block.  */
  813.  
  814.   sort_all_symtab_syms ();
  815.  
  816.   /* Go over the misc symbol bunches and install them in vector.  */
  817.  
  818.   condense_misc_bunches (!mainline);
  819.  
  820.   /* Make a default for file to list.  */
  821.  
  822.   select_source_symtab (0);    /* FIXME, this might be too slow, see dbxread */
  823. }
  824.  
  825. static void
  826. coff_new_init ()
  827. {
  828.   /* There seems to be nothing to do except free_all_symtabs and set
  829.      symfile to zero, which is done by our caller.  */
  830. }
  831.  
  832. /* Simplified internal version of coff symbol table information */
  833.  
  834. struct coff_symbol {
  835.   char *c_name;
  836.   int c_symnum;        /* symbol number of this entry */
  837.   int c_nsyms;        /* 1 if syment only, 2 if syment + auxent, etc */
  838.   long c_value;
  839.   int c_sclass;
  840.   int c_secnum;
  841.   unsigned int c_type;
  842. };
  843.  
  844. /* Given pointers to a symbol table in coff style exec file,
  845.    analyze them and create struct symtab's describing the symbols.
  846.    NSYMS is the number of symbols in the symbol table.
  847.    We read them one at a time using read_one_sym ().  */
  848.  
  849. static void
  850. read_coff_symtab (desc, nsyms)
  851.      int desc;
  852.      int nsyms;
  853. {
  854.   int newfd;            /* Avoid multiple closes on same desc */
  855.   FILE *stream; 
  856.   register struct context_stack *new;
  857.   struct coff_symbol coff_symbol;
  858.   register struct coff_symbol *cs = &coff_symbol;
  859.   static struct internal_syment main_sym;
  860.   static union internal_auxent main_aux;
  861.   struct coff_symbol fcn_cs_saved;
  862.   static struct internal_syment fcn_sym_saved;
  863.   static union internal_auxent fcn_aux_saved;
  864.  
  865.   /* A .file is open.  */
  866.   int in_source_file = 0;
  867.   int num_object_files = 0;
  868.   int next_file_symnum = -1;
  869.  
  870.   /* Name of the current file.  */
  871.   char *filestring = "";
  872.   int depth;
  873.   int fcn_first_line;
  874.   int fcn_last_line;
  875.   int fcn_start_addr;
  876.   long fcn_line_ptr;
  877.   struct cleanup *old_chain;
  878.  
  879.  
  880.   newfd = dup (desc);
  881.   if (newfd == -1)
  882.     fatal ("Too many open files");
  883.   stream = fdopen (newfd, "r");
  884.  
  885.   old_chain = make_cleanup (free_all_symtabs, 0);
  886.   make_cleanup (fclose, stream);
  887.   nlist_stream_global = stream;
  888.   nlist_nsyms_global = nsyms;
  889.   last_source_file = 0;
  890.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  891.  
  892.   if (type_vector)            /* Get rid of previous one */
  893.     free (type_vector);
  894.   type_vector_length = 160;
  895.   type_vector = (struct type **)
  896.         xmalloc (type_vector_length * sizeof (struct type *));
  897.   bzero (type_vector, type_vector_length * sizeof (struct type *));
  898.  
  899.   start_symtab ();
  900.  
  901.   symnum = 0;
  902.   while (symnum < nsyms)
  903.     {
  904.       QUIT;            /* Make this command interruptable.  */
  905.       read_one_sym (cs, &main_sym, &main_aux);
  906.  
  907. #ifdef SEM
  908.       temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
  909.                      cs->c_name[2] << 8 | cs->c_name[3];
  910.       if (int_sem_val == temp_sem_val)
  911.         last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
  912. #endif
  913.  
  914.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  915.     {
  916.       if (last_source_file)
  917.         end_symtab ();
  918.  
  919.       start_symtab ();
  920.       complete_symtab ("_globals_", 0, first_object_file_end);
  921.       /* done with all files, everything from here on out is globals */
  922.     }
  923.  
  924.       /* Special case for file with type declarations only, no text.  */
  925.       if (!last_source_file && SDB_TYPE (cs->c_type)
  926.       && cs->c_secnum == N_DEBUG)
  927.     complete_symtab (filestring, 0, 0);
  928.  
  929.       /* Typedefs should not be treated as symbol definitions.  */
  930.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  931.     {
  932.       /* record as misc function.  if we get '.bf' next,
  933.        * then we undo this step
  934.        */
  935.       record_misc_function (cs->c_name, cs->c_value);
  936.  
  937.       fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  938.       fcn_start_addr = cs->c_value;
  939.       fcn_cs_saved = *cs;
  940.       fcn_sym_saved = main_sym;
  941.       fcn_aux_saved = main_aux;
  942.       continue;
  943.     }
  944.  
  945.       switch (cs->c_sclass)
  946.     {
  947.       case C_EFCN:
  948.       case C_EXTDEF:
  949.       case C_ULABEL:
  950.       case C_USTATIC:
  951.       case C_LINE:
  952.       case C_ALIAS:
  953.       case C_HIDDEN:
  954.         printf ("Bad n_sclass = %d\n", cs->c_sclass);
  955.         break;
  956.  
  957.       case C_FILE:
  958.         /*
  959.          * c_value field contains symnum of next .file entry in table
  960.          * or symnum of first global after last .file.
  961.          */
  962.         next_file_symnum = cs->c_value;
  963.         filestring = getfilename (&main_aux);
  964.         /*
  965.          * Complete symbol table for last object file
  966.          * containing debugging information.
  967.          */
  968.         if (last_source_file)
  969.           {
  970.         end_symtab ();
  971.         start_symtab ();
  972.           }
  973.         in_source_file = 1;
  974.         break;
  975.  
  976.           case C_STAT:
  977.         if (cs->c_name[0] == '.') {
  978.             if (strcmp (cs->c_name, ".text") == 0) {
  979.                 /* FIXME:  don't wire in ".text" as section name
  980.                        or symbol name! */
  981.                 if (++num_object_files == 1) {
  982.                     /* last address of startup file */
  983.                     first_object_file_end = cs->c_value +
  984.                         main_aux.x_scn.x_scnlen;
  985.                 }
  986.                 /* Check for in_source_file deals with case of
  987.                    a file with debugging symbols
  988.                    followed by a later file with no symbols.  */
  989.                 if (in_source_file)
  990.                   complete_symtab (filestring, cs->c_value,
  991.                            main_aux.x_scn.x_scnlen);
  992.                 in_source_file = 0;
  993.             }
  994.             /* flush rest of '.' symbols */
  995.             break;
  996.         }
  997.         else if (!SDB_TYPE (cs->c_type)
  998.              && cs->c_name[0] == 'L'
  999.              && (strncmp (cs->c_name, "LI%", 3) == 0
  1000.              || strncmp (cs->c_name, "LF%", 3) == 0
  1001.              || strncmp (cs->c_name,"LC%",3) == 0
  1002.              || strncmp (cs->c_name,"LP%",3) == 0
  1003.              || strncmp (cs->c_name,"LPB%",4) == 0
  1004.              || strncmp (cs->c_name,"LBB%",4) == 0
  1005.              || strncmp (cs->c_name,"LBE%",4) == 0
  1006.              || strncmp (cs->c_name,"LPBX%",5) == 0))
  1007.           /* At least on a 3b1, gcc generates swbeg and string labels
  1008.          that look like this.  Ignore them.  */
  1009.           break;
  1010.         /* fall in for static symbols that don't start with '.' */
  1011.       case C_EXT:
  1012.         if (!SDB_TYPE (cs->c_type)) {
  1013.         /* FIXME: This is BOGUS Will Robinson! 
  1014.          Coff should provide the SEC_CODE flag for executable sections,
  1015.          then if we could look up sections by section number we
  1016.            could see if the flags indicate SEC_CODE.  If so, then
  1017.          record this symbol as a miscellaneous function.  But why
  1018.         are absolute syms recorded as functions, anyway?  */    
  1019.             if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
  1020.                 record_misc_function (cs->c_name, cs->c_value);
  1021.                 break;
  1022.             } else {
  1023.                 cs->c_type = T_INT;
  1024.             }
  1025.         }
  1026.         (void) process_coff_symbol (cs, &main_aux);
  1027.         break;
  1028.  
  1029.       case C_FCN:
  1030.         if (strcmp (cs->c_name, ".bf") == 0)
  1031.           {
  1032.         within_function = 1;
  1033.  
  1034.         /* value contains address of first non-init type code */
  1035.         /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1036.                 contains line number of '{' } */
  1037.         fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  1038.  
  1039.         new = (struct context_stack *)
  1040.           xmalloc (sizeof (struct context_stack));
  1041.         new->depth = depth = 0;
  1042.         new->next = 0;
  1043.         context_stack = new;
  1044.         new->locals = 0;
  1045.         new->old_blocks = pending_blocks;
  1046.         new->start_addr = fcn_start_addr;
  1047.         fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
  1048.         new->name = process_coff_symbol (&fcn_cs_saved,
  1049.                          &fcn_aux_saved);
  1050.           }
  1051.         else if (strcmp (cs->c_name, ".ef") == 0)
  1052.           {
  1053.               /* the value of .ef is the address of epilogue code;
  1054.                * not useful for gdb
  1055.                */
  1056.         /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1057.                 contains number of lines to '}' */
  1058.         new = context_stack;
  1059.         if (new == 0)
  1060.           {
  1061.             complain (&ef_complaint, cs->c_symnum);
  1062.             within_function = 0;
  1063.             break;
  1064.           }
  1065.         fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  1066.         enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
  1067.  
  1068.         finish_block (new->name, &local_symbols, new->old_blocks,
  1069.                   new->start_addr,
  1070. #if defined (FUNCTION_EPILOGUE_SIZE)
  1071.                   /* This macro should be defined only on
  1072.                  machines where the
  1073.                  fcn_aux_saved.x_sym.x_misc.x_fsize
  1074.                  field is always zero.
  1075.                  So use the .bf record information that
  1076.                  points to the epilogue and add the size
  1077.                  of the epilogue.  */
  1078.                   cs->c_value + FUNCTION_EPILOGUE_SIZE
  1079. #else
  1080.                   fcn_cs_saved.c_value +
  1081.                       fcn_aux_saved.x_sym.x_misc.x_fsize
  1082. #endif
  1083.                   );
  1084.         context_stack = 0;
  1085.         within_function = 0;
  1086.         free (new);
  1087.           }
  1088.         break;
  1089.  
  1090.       case C_BLOCK:
  1091.         if (strcmp (cs->c_name, ".bb") == 0)
  1092.           {
  1093.         new = (struct context_stack *)
  1094.                 xmalloc (sizeof (struct context_stack));
  1095.         depth++;
  1096.         new->depth = depth;
  1097.         new->next = context_stack;
  1098.         context_stack = new;
  1099.         new->locals = local_symbols;
  1100.         new->old_blocks = pending_blocks;
  1101.         new->start_addr = cs->c_value;
  1102.         new->name = 0;
  1103.         local_symbols = 0;
  1104.           }
  1105.         else if (strcmp (cs->c_name, ".eb") == 0)
  1106.           {
  1107.         new = context_stack;
  1108.         if (new == 0 || depth != new->depth)
  1109.           error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
  1110.              symnum);
  1111.         if (local_symbols && context_stack->next)
  1112.           {
  1113.             /* Make a block for the local symbols within.  */
  1114.             finish_block (0, &local_symbols, new->old_blocks,
  1115.                   new->start_addr, cs->c_value);
  1116.           }
  1117.         depth--;
  1118.         local_symbols = new->locals;
  1119.         context_stack = new->next;
  1120.         free (new);
  1121.           }
  1122.         break;
  1123. #ifdef TDESC
  1124.           case C_VERSION:
  1125. #if 0
  1126.         /* This isn't used currently.  */
  1127.             if (strcmp (cs->c_name, ".coffsyn") == 0)
  1128.         last_coffsyn = cs->c_value;
  1129.         else
  1130. #endif /* 0 */
  1131.           if ((strcmp (cs->c_name, ".coffsem") == 0) &&
  1132.                      (cs->c_value != 0))
  1133.         last_coffsem = cs->c_value;
  1134.             break;
  1135. #endif /* TDESC */
  1136.  
  1137.       default:
  1138. #ifdef TDESC
  1139.         if ((strcmp (cs->c_name, ".coffsem") == 0) &&
  1140.         (cs->c_value != 0))
  1141.           last_coffsem = cs->c_value;
  1142.             else
  1143. #endif
  1144.         (void) process_coff_symbol (cs, &main_aux);
  1145.         break;
  1146.     }
  1147.     }
  1148.  
  1149.   if (last_source_file)
  1150.     end_symtab ();
  1151.   fclose (stream);
  1152.   discard_cleanups (old_chain);
  1153. }
  1154.  
  1155. /* Routines for reading headers and symbols from executable.  */
  1156.  
  1157. #ifdef FIXME
  1158. /* Move these XXXMAGIC symbol defns into BFD!  */
  1159.  
  1160. /* Read COFF file header, check magic number,
  1161.    and return number of symbols. */
  1162. read_file_hdr (chan, file_hdr)
  1163.     int chan;
  1164.     FILHDR *file_hdr;
  1165. {
  1166.   lseek (chan, 0L, 0);
  1167.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1168.     return -1;
  1169.  
  1170.   switch (file_hdr->f_magic)
  1171.     {
  1172. #ifdef MC68MAGIC
  1173.     case MC68MAGIC:
  1174. #endif
  1175. #ifdef NS32GMAGIC
  1176.       case NS32GMAGIC:
  1177.       case NS32SMAGIC:
  1178. #endif
  1179. #ifdef I386MAGIC
  1180.     case I386MAGIC:
  1181. #endif
  1182. #ifdef CLIPPERMAGIC
  1183.     case CLIPPERMAGIC:
  1184. #endif
  1185. #if defined (MC68KWRMAGIC) \
  1186.   && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
  1187.     case MC68KWRMAGIC:
  1188. #endif
  1189. #ifdef MC68KROMAGIC
  1190.     case MC68KROMAGIC:
  1191.     case MC68KPGMAGIC:
  1192. #endif
  1193. #ifdef MC88DGMAGIC
  1194.     case MC88DGMAGIC:
  1195. #endif      
  1196. #ifdef MC88MAGIC
  1197.     case MC88MAGIC:
  1198. #endif      
  1199. #ifdef I960ROMAGIC
  1200.     case I960ROMAGIC:        /* Intel 960 */
  1201. #endif
  1202. #ifdef I960RWMAGIC
  1203.     case I960RWMAGIC:        /* Intel 960 */
  1204. #endif
  1205.     return file_hdr->f_nsyms;
  1206.  
  1207.       default:
  1208. #ifdef BADMAG
  1209.     if (BADMAG(file_hdr))
  1210.       return -1;
  1211.     else
  1212.       return file_hdr->f_nsyms;
  1213. #else
  1214.     return -1;
  1215. #endif
  1216.     }
  1217. }
  1218. #endif
  1219.  
  1220. /* Read the next symbol, swap it, and return it in both internal_syment
  1221.    form, and coff_symbol form.  Also return its first auxent, if any,
  1222.    in internal_auxent form, and skip any other auxents.  */
  1223.  
  1224. static void
  1225. read_one_sym (cs, sym, aux)
  1226.     register struct coff_symbol *cs;
  1227.     register struct internal_syment *sym;
  1228.     register union internal_auxent *aux;
  1229. {
  1230.   int i;
  1231.  
  1232.   cs->c_symnum = symnum;
  1233.   fread (temp_sym, local_symesz, 1, nlist_stream_global);
  1234.   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
  1235.   cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
  1236.   if (cs->c_nsyms >= 2)
  1237.     {
  1238.     fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1239.     bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
  1240.               (char *)aux);
  1241.     /* If more than one aux entry, read past it (only the first aux
  1242.        is important). */
  1243.     for (i = 2; i < cs->c_nsyms; i++)
  1244.       fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1245.     }
  1246.   cs->c_name = getsymname (sym);
  1247.   cs->c_value = sym->n_value;
  1248.   cs->c_sclass = (sym->n_sclass & 0xff);
  1249.   cs->c_secnum = sym->n_scnum;
  1250.   cs->c_type = (unsigned) sym->n_type;
  1251.   if (!SDB_TYPE (cs->c_type))
  1252.     cs->c_type = 0;
  1253.  
  1254.   symnum += cs->c_nsyms;
  1255. }
  1256.  
  1257. /* Support for string table handling */
  1258.  
  1259. static char *stringtab = NULL;
  1260.  
  1261. static int
  1262. init_stringtab (chan, offset)
  1263.     int chan;
  1264.     long offset;
  1265. {
  1266.   long length;
  1267.   int val;
  1268.   unsigned char lengthbuf[4];
  1269.  
  1270.   if (stringtab)
  1271.     {
  1272.       free (stringtab);
  1273.       stringtab = NULL;
  1274.     }
  1275.  
  1276.   if (lseek (chan, offset, 0) < 0)
  1277.     return -1;
  1278.  
  1279.   val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
  1280.   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
  1281.  
  1282.   /* If no string table is needed, then the file may end immediately
  1283.      after the symbols.  Just return with `stringtab' set to null. */
  1284.   if (val != sizeof length || length < sizeof length)
  1285.     return 0;
  1286.  
  1287.   stringtab = (char *) xmalloc (length);
  1288.   if (stringtab == NULL)
  1289.     return -1;
  1290.  
  1291.   bcopy (&length, stringtab, sizeof length);
  1292.   if (length == sizeof length)        /* Empty table -- just the count */
  1293.     return 0;
  1294.  
  1295.   val = myread (chan, stringtab + sizeof length, length - sizeof length);
  1296.   if (val != length - sizeof length || stringtab[length - 1] != '\0')
  1297.     return -1;
  1298.  
  1299.   return 0;
  1300. }
  1301.  
  1302. static void
  1303. free_stringtab ()
  1304. {
  1305.   if (stringtab)
  1306.     free (stringtab);
  1307.   stringtab = NULL;
  1308. }
  1309.  
  1310. static char *
  1311. getsymname (symbol_entry)
  1312.     struct internal_syment *symbol_entry;
  1313. {
  1314.   static char buffer[SYMNMLEN+1];
  1315.   char *result;
  1316.  
  1317.   if (symbol_entry->_n._n_n._n_zeroes == 0)
  1318.     {
  1319.       result = stringtab + symbol_entry->_n._n_n._n_offset;
  1320.     }
  1321.   else
  1322.     {
  1323.       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
  1324.       buffer[SYMNMLEN] = '\0';
  1325.       result = buffer;
  1326.     }
  1327.   return result;
  1328. }
  1329.  
  1330. static char *
  1331. getfilename (aux_entry)
  1332.     union internal_auxent *aux_entry;
  1333. {
  1334.   static char buffer[BUFSIZ];
  1335.   register char *temp;
  1336.   char *result;
  1337.   extern char *rindex ();
  1338.  
  1339. #ifndef COFF_NO_LONG_FILE_NAMES
  1340. #if defined (x_zeroes)
  1341.   /* Data General.  */
  1342.   if (aux_entry->x_zeroes == 0)
  1343.     strcpy (buffer, stringtab + aux_entry->x_offset);
  1344. #else /* no x_zeroes */
  1345.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1346.     strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
  1347. #endif /* no x_zeroes */
  1348.   else
  1349. #endif /* COFF_NO_LONG_FILE_NAMES */
  1350.     {
  1351. #if defined (x_name)
  1352.       /* Data General.  */
  1353.       strncpy (buffer, aux_entry->x_name, FILNMLEN);
  1354. #else
  1355.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1356. #endif
  1357.       buffer[FILNMLEN] = '\0';
  1358.     }
  1359.   result = buffer;
  1360.   if ((temp = rindex (result, '/')) != NULL)
  1361.     result = temp + 1;
  1362.   return (result);
  1363. }
  1364.  
  1365. /* Support for line number handling */
  1366. static char *linetab = NULL;
  1367. static long linetab_offset;
  1368. static unsigned long linetab_size;
  1369.  
  1370. /* Read in all the line numbers for fast lookups later.  Leave them in
  1371.    external (unswapped) format in memory; we'll swap them as we enter
  1372.    them into GDB's data structures.  */
  1373.  
  1374. static int
  1375. init_lineno (chan, offset, size)
  1376.     int chan;
  1377.     long offset;
  1378.     int size;
  1379. {
  1380.   int val;
  1381.  
  1382.   linetab_offset = offset;
  1383.   linetab_size = size;
  1384.  
  1385.   if (size == 0)
  1386.     return 0;
  1387.  
  1388.   if (lseek (chan, offset, 0) < 0)
  1389.     return -1;
  1390.   
  1391.   linetab = (char *) xmalloc (size);
  1392.  
  1393.   val = myread (chan, linetab, size);
  1394.   if (val != size)
  1395.     return -1;
  1396.  
  1397.   make_cleanup (free, linetab);        /* Be sure it gets de-allocated. */
  1398.   return 0;
  1399. }
  1400.  
  1401. #if !defined (L_LNNO32)
  1402. #define L_LNNO32(lp) ((lp)->l_lnno)
  1403. #endif
  1404.  
  1405. static void
  1406. enter_linenos (file_offset, first_line, last_line)
  1407.     long file_offset;
  1408.     register int first_line;
  1409.     register int last_line;
  1410. {
  1411.   register char *rawptr;
  1412.   struct internal_lineno lptr;
  1413.  
  1414.   if (file_offset < linetab_offset)
  1415.     {
  1416.       complain (&lineno_complaint, file_offset);
  1417.       if (file_offset > linetab_size)    /* Too big to be an offset? */
  1418.     return;
  1419.       file_offset += linetab_offset;  /* Try reading at that linetab offset */
  1420.     }
  1421.   
  1422.   rawptr = &linetab[file_offset - linetab_offset];
  1423.  
  1424.   /* skip first line entry for each function */
  1425.   rawptr += local_linesz;
  1426.   /* line numbers start at one for the first line of the function */
  1427.   first_line--;
  1428.  
  1429.   for (;;) {
  1430.     bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
  1431.     rawptr += local_linesz;
  1432.     if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
  1433.       record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
  1434.     else
  1435.       break;
  1436.   } 
  1437. }
  1438.  
  1439. static int
  1440. hashname (name)
  1441.      char *name;
  1442. {
  1443.   register char *p = name;
  1444.   register int total = p[0];
  1445.   register int c;
  1446.  
  1447.   c = p[1];
  1448.   total += c << 2;
  1449.   if (c)
  1450.     {
  1451.       c = p[2];
  1452.       total += c << 4;
  1453.       if (c)
  1454.     total += p[3] << 6;
  1455.     }
  1456.   
  1457.   return total % HASHSIZE;
  1458. }
  1459.  
  1460. static void
  1461. patch_type (type, real_type)
  1462.     struct type *type;
  1463.     struct type *real_type;
  1464. {
  1465.   register struct type *target = TYPE_TARGET_TYPE (type);
  1466.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1467.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1468.  
  1469.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1470.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1471.   TYPE_FIELDS (target) = (struct field *)
  1472.                 obstack_alloc (symbol_obstack, field_size);
  1473.  
  1474.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1475.  
  1476.   if (TYPE_NAME (real_target))
  1477.     {
  1478.       if (TYPE_NAME (target))
  1479.     free (TYPE_NAME (target));
  1480.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
  1481.     }
  1482. }
  1483.  
  1484. /* Patch up all appropriate typdef symbols in the opaque_type_chains
  1485.    so that they can be used to print out opaque data structures properly */
  1486.  
  1487. static void
  1488. patch_opaque_types ()
  1489. {
  1490.   struct symtab *s;
  1491.  
  1492.   /* Look at each symbol in the per-file block of each symtab.  */
  1493.   for (s = symtab_list; s; s = s->next)
  1494.     {
  1495.       register struct block *b;
  1496.       register int i;
  1497.  
  1498.       /* Go through the per-file symbols only */
  1499.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  1500.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1501.     {
  1502.       register struct symbol *real_sym;
  1503.  
  1504.       /* Find completed typedefs to use to fix opaque ones.
  1505.          Remove syms from the chain when their types are stored,
  1506.          but search the whole chain, as there may be several syms
  1507.          from different files with the same name.  */
  1508.       real_sym = BLOCK_SYM (b, i);
  1509.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  1510.           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  1511.           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  1512.           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1513.         {
  1514.           register char *name = SYMBOL_NAME (real_sym);
  1515.           register int hash = hashname (name);
  1516.           register struct symbol *sym, *prev;
  1517.  
  1518.           prev = 0;
  1519.           for (sym = opaque_type_chain[hash]; sym;)
  1520.         {
  1521.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  1522.               !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  1523.             {
  1524.               if (prev)
  1525.             SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
  1526.               else
  1527.             opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
  1528.  
  1529.               patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  1530.  
  1531.               if (prev)
  1532.             sym = SYMBOL_VALUE_CHAIN (prev);
  1533.               else
  1534.             sym = opaque_type_chain[hash];
  1535.             }
  1536.           else
  1537.             {
  1538.               prev = sym;
  1539.               sym = SYMBOL_VALUE_CHAIN (sym);
  1540.             }
  1541.         }
  1542.         }
  1543.     }
  1544.     }
  1545. }
  1546.  
  1547. #if defined (clipper)
  1548. #define BELIEVE_PCC_PROMOTION 1
  1549. #endif
  1550.  
  1551. static struct symbol *
  1552. process_coff_symbol (cs, aux)
  1553.      register struct coff_symbol *cs;
  1554.      register union internal_auxent *aux;
  1555. {
  1556.   register struct symbol *sym
  1557.     = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
  1558.   char *name;
  1559. #ifdef NAMES_HAVE_UNDERSCORE
  1560.   int offset = 1;
  1561. #else
  1562.   int offset = 0;
  1563. #endif
  1564.  
  1565.   bzero (sym, sizeof (struct symbol));
  1566.   name = cs->c_name;
  1567.   name = (name[0] == '_' ? name + offset : name);
  1568.   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
  1569.  
  1570.   /* default assumptions */
  1571.   SYMBOL_VALUE (sym) = cs->c_value;
  1572.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1573.  
  1574.   if (ISFCN (cs->c_type))
  1575.     {
  1576. #if 0
  1577.        /* FIXME:  This has NOT been tested.  The DBX version has.. */
  1578.        /* Generate a template for the type of this function.  The 
  1579.       types of the arguments will be added as we read the symbol 
  1580.       table. */
  1581.        struct type *new = (struct type *)
  1582.             obstack_alloc (symbol_obstack, sizeof (struct type));
  1583.        
  1584.        bcopy(lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
  1585.          new, sizeof(struct type));
  1586.        SYMBOL_TYPE (sym) = new;
  1587.        in_function_type = SYMBOL_TYPE(sym);
  1588. #else
  1589.        SYMBOL_TYPE(sym) = 
  1590.      lookup_function_type (decode_function_type (cs, cs->c_type, aux));
  1591. #endif
  1592.  
  1593.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  1594.       if (cs->c_sclass == C_STAT)
  1595.     add_symbol_to_list (sym, &file_symbols);
  1596.       else if (cs->c_sclass == C_EXT)
  1597.     add_symbol_to_list (sym, &global_symbols);
  1598.     }
  1599.   else
  1600.     {
  1601.       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
  1602.       switch (cs->c_sclass)
  1603.     {
  1604.       case C_NULL:
  1605.         break;
  1606.  
  1607.       case C_AUTO:
  1608.         SYMBOL_CLASS (sym) = LOC_LOCAL;
  1609.         add_symbol_to_list (sym, &local_symbols);
  1610.         break;
  1611.  
  1612.       case C_EXT:
  1613.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1614.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1615.         add_symbol_to_list (sym, &global_symbols);
  1616.         break;
  1617.  
  1618.       case C_STAT:
  1619.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1620.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1621.         if (within_function) {
  1622.           /* Static symbol of local scope */
  1623.           add_symbol_to_list (sym, &local_symbols);
  1624.         }
  1625.         else {
  1626.           /* Static symbol at top level of file */
  1627.           add_symbol_to_list (sym, &file_symbols);
  1628.         }
  1629.         break;
  1630.  
  1631. #ifdef C_GLBLREG        /* AMD coff */
  1632.       case C_GLBLREG:
  1633. #endif
  1634.       case C_REG:
  1635.         SYMBOL_CLASS (sym) = LOC_REGISTER;
  1636.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1637.         add_symbol_to_list (sym, &local_symbols);
  1638.         break;
  1639.  
  1640.       case C_LABEL:
  1641.         break;
  1642.  
  1643.       case C_ARG:
  1644.         SYMBOL_CLASS (sym) = LOC_ARG;
  1645. #if 0
  1646.         /* FIXME:  This has not bee tested. */
  1647.         /* Add parameter to function.  */
  1648.         add_param_to_type(&in_function_type,sym);
  1649. #endif
  1650.         add_symbol_to_list (sym, &local_symbols);
  1651. #if !defined (BELIEVE_PCC_PROMOTION)
  1652.         /* If PCC says a parameter is a short or a char,
  1653.            it is really an int.  */
  1654.         if (SYMBOL_TYPE (sym) == builtin_type_char
  1655.         || SYMBOL_TYPE (sym) == builtin_type_short)
  1656.           SYMBOL_TYPE (sym) = builtin_type_int;
  1657.         else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
  1658.              || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  1659.           SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
  1660. #endif
  1661.         break;
  1662.  
  1663.       case C_REGPARM:
  1664.         SYMBOL_CLASS (sym) = LOC_REGPARM;
  1665.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1666.         add_symbol_to_list (sym, &local_symbols);
  1667. #if !defined (BELIEVE_PCC_PROMOTION)
  1668.         /* If PCC says a parameter is a short or a char,
  1669.            it is really an int.  */
  1670.         if (SYMBOL_TYPE (sym) == builtin_type_char
  1671.         || SYMBOL_TYPE (sym) == builtin_type_short)
  1672.           SYMBOL_TYPE (sym) = builtin_type_int;
  1673.         else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
  1674.              || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  1675.           SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
  1676. #endif
  1677.         break;
  1678.         
  1679.       case C_TPDEF:
  1680.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1681.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1682.  
  1683.         /* If type has no name, give it one */
  1684.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0 
  1685.         && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1686.           TYPE_NAME (SYMBOL_TYPE (sym))
  1687.                       = concat (SYMBOL_NAME (sym), "", "");
  1688.  
  1689.         /* Keep track of any type which points to empty structured type,
  1690.         so it can be filled from a definition from another file */
  1691.         if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
  1692.         TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
  1693.           {
  1694.         register int i = hashname (SYMBOL_NAME (sym));
  1695.  
  1696.         SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
  1697.         opaque_type_chain[i] = sym;
  1698.           }
  1699.         add_symbol_to_list (sym, &file_symbols);
  1700.         break;
  1701.  
  1702.       case C_STRTAG:
  1703.       case C_UNTAG:
  1704.       case C_ENTAG:
  1705.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1706.         SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1707.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  1708.         && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1709.           TYPE_NAME (SYMBOL_TYPE (sym))
  1710.         = concat ("",
  1711.               (cs->c_sclass == C_ENTAG
  1712.                ? "enum "
  1713.                : (cs->c_sclass == C_STRTAG
  1714.                   ? "struct " : "union ")),
  1715.               SYMBOL_NAME (sym));
  1716.         add_symbol_to_list (sym, &file_symbols);
  1717.         break;
  1718.  
  1719.       default:
  1720.         break;
  1721.     }
  1722.     }
  1723.   return sym;
  1724. }
  1725.  
  1726. /* Decode a coff type specifier;
  1727.    return the type that is meant.  */
  1728.  
  1729. static
  1730. struct type *
  1731. decode_type (cs, c_type, aux)
  1732.      register struct coff_symbol *cs;
  1733.      unsigned int c_type;
  1734.      register union internal_auxent *aux;
  1735. {
  1736.   register struct type *type = 0;
  1737.   unsigned int new_c_type;
  1738.  
  1739.   if (c_type & ~N_BTMASK)
  1740.     {
  1741.       new_c_type = DECREF (c_type);
  1742.       if (ISPTR (c_type))
  1743.     {
  1744.       type = decode_type (cs, new_c_type, aux);
  1745.       type = lookup_pointer_type (type);
  1746.     }
  1747.       else if (ISFCN (c_type))
  1748.     {
  1749.       type = decode_type (cs, new_c_type, aux);
  1750.       type = lookup_function_type (type);
  1751.     }
  1752.       else if (ISARY (c_type))
  1753.     {
  1754.       int i, n;
  1755.       register unsigned short *dim;
  1756.       struct type *base_type;
  1757.  
  1758.       /* Define an array type.  */
  1759.       /* auxent refers to array, not base type */
  1760.       if (aux->x_sym.x_tagndx.l == 0)
  1761.         cs->c_nsyms = 1;
  1762.  
  1763.       /* shift the indices down */
  1764.       dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
  1765.       i = 1;
  1766.       n = dim[0];
  1767.       for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
  1768.         *dim = *(dim + 1);
  1769.       *dim = 0;
  1770.  
  1771.       type = (struct type *)
  1772.             obstack_alloc (symbol_obstack, sizeof (struct type));
  1773.       bzero (type, sizeof (struct type));
  1774.  
  1775.       base_type = decode_type (cs, new_c_type, aux);
  1776.  
  1777.       TYPE_CODE (type) = TYPE_CODE_ARRAY;
  1778.       TYPE_TARGET_TYPE (type) = base_type;
  1779.       TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
  1780.     }
  1781.       return type;
  1782.     }
  1783.  
  1784.   /* Reference to existing type */
  1785.   if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx.l != 0)
  1786.     {
  1787.       type = coff_alloc_type (aux->x_sym.x_tagndx.l);
  1788.       return type;
  1789.     }
  1790.  
  1791.   return decode_base_type (cs, BTYPE (c_type), aux);
  1792. }
  1793.  
  1794. /* Decode a coff type specifier for function definition;
  1795.    return the type that the function returns.  */
  1796.  
  1797. static
  1798. struct type *
  1799. decode_function_type (cs, c_type, aux)
  1800.      register struct coff_symbol *cs;
  1801.      unsigned int c_type;
  1802.      register union internal_auxent *aux;
  1803. {
  1804.   if (aux->x_sym.x_tagndx.l == 0)
  1805.     cs->c_nsyms = 1;    /* auxent refers to function, not base type */
  1806.  
  1807.   return decode_type (cs, DECREF (c_type), aux);
  1808. }
  1809.  
  1810. /* basic C types */
  1811.  
  1812. static
  1813. struct type *
  1814. decode_base_type (cs, c_type, aux)
  1815.      register struct coff_symbol *cs;
  1816.      unsigned int c_type;
  1817.      register union internal_auxent *aux;
  1818. {
  1819.   struct type *type;
  1820.  
  1821.   switch (c_type)
  1822.     {
  1823.       case T_NULL:
  1824.         /* shows up with "void (*foo)();" structure members */
  1825.     return builtin_type_void;
  1826.  
  1827. #if 0
  1828. /* DGUX actually defines both T_ARG and T_VOID to the same value.  */
  1829. #ifdef T_ARG
  1830.       case T_ARG:
  1831.     /* Shows up in DGUX, I think.  Not sure where.  */
  1832.     return builtin_type_void;    /* shouldn't show up here */
  1833. #endif
  1834. #endif /* 0 */
  1835.  
  1836. #ifdef T_VOID
  1837.       case T_VOID:
  1838.     /* Intel 960 COFF has this symbol and meaning.  */
  1839.     return builtin_type_void;
  1840. #endif
  1841.  
  1842.       case T_CHAR:
  1843.     return builtin_type_char;
  1844.  
  1845.       case T_SHORT:
  1846.     return builtin_type_short;
  1847.  
  1848.       case T_INT:
  1849.     return builtin_type_int;
  1850.  
  1851.       case T_LONG:
  1852.     return builtin_type_long;
  1853.  
  1854.       case T_FLOAT:
  1855.     return builtin_type_float;
  1856.  
  1857.       case T_DOUBLE:
  1858.     return builtin_type_double;
  1859.  
  1860.       case T_STRUCT:
  1861.     if (cs->c_nsyms != 2)
  1862.       {
  1863.         /* anonymous structure type */
  1864.         type = coff_alloc_type (cs->c_symnum);
  1865.         TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1866.         TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
  1867.         TYPE_LENGTH (type) = 0;
  1868.         TYPE_FIELDS (type) = 0;
  1869.         TYPE_NFIELDS (type) = 0;
  1870.       }
  1871.     else
  1872.       {
  1873.         type = read_struct_type (cs->c_symnum,
  1874.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1875.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1876.       }
  1877.     return type;
  1878.  
  1879.       case T_UNION:
  1880.     if (cs->c_nsyms != 2)
  1881.       {
  1882.         /* anonymous union type */
  1883.         type = coff_alloc_type (cs->c_symnum);
  1884.         TYPE_NAME (type) = concat ("union ", "<opaque>", "");
  1885.         TYPE_LENGTH (type) = 0;
  1886.         TYPE_FIELDS (type) = 0;
  1887.         TYPE_NFIELDS (type) = 0;
  1888.       }
  1889.     else
  1890.       {
  1891.         type = read_struct_type (cs->c_symnum,
  1892.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1893.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1894.       }
  1895.     TYPE_CODE (type) = TYPE_CODE_UNION;
  1896.     return type;
  1897.  
  1898.       case T_ENUM:
  1899.     return read_enum_type (cs->c_symnum,
  1900.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1901.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1902.  
  1903.       case T_MOE:
  1904.     /* shouldn't show up here */
  1905.     break;
  1906.  
  1907.       case T_UCHAR:
  1908.     return builtin_type_unsigned_char;
  1909.  
  1910.       case T_USHORT:
  1911.     return builtin_type_unsigned_short;
  1912.  
  1913.       case T_UINT:
  1914.     return builtin_type_unsigned_int;
  1915.  
  1916.       case T_ULONG:
  1917.     return builtin_type_unsigned_long;
  1918.     }
  1919.   printf ("unexpected type %d at symnum %d\n", c_type, cs->c_symnum);
  1920.   return builtin_type_void;
  1921. }
  1922.  
  1923. /* This page contains subroutines of read_type.  */
  1924.  
  1925. /* Read the description of a structure (or union type)
  1926.    and return an object describing the type.  */
  1927.  
  1928. static struct type *
  1929. read_struct_type (index, length, lastsym)
  1930.      int index;
  1931.      int length;
  1932.      int lastsym;
  1933. {
  1934.   struct nextfield
  1935.     {
  1936.       struct nextfield *next;
  1937.       struct field field;
  1938.     };
  1939.  
  1940.   register struct type *type;
  1941.   register struct nextfield *list = 0;
  1942.   struct nextfield *new;
  1943.   int nfields = 0;
  1944.   register int n;
  1945.   char *name;
  1946. #ifdef NAMES_HAVE_UNDERSCORE
  1947.   int offset = 1;
  1948. #else
  1949.   int offset = 0;
  1950. #endif
  1951.   struct coff_symbol member_sym;
  1952.   register struct coff_symbol *ms = &member_sym;
  1953.   struct internal_syment sub_sym;
  1954.   union internal_auxent sub_aux;
  1955.   int done = 0;
  1956.  
  1957.   type = coff_alloc_type (index);
  1958.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1959.   TYPE_LENGTH (type) = length;
  1960.  
  1961.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1962.     {
  1963.       read_one_sym (ms, &sub_sym, &sub_aux);
  1964.       name = ms->c_name;
  1965.       name = (name[0] == '_' ? name + offset : name);
  1966.  
  1967.       switch (ms->c_sclass)
  1968.     {
  1969.       case C_MOS:
  1970.       case C_MOU:
  1971.  
  1972.         /* Get space to record the next field's data.  */
  1973.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1974.         new->next = list;
  1975.         list = new;
  1976.  
  1977.         /* Save the data.  */
  1978.         list->field.name = savestring (name, strlen (name));
  1979.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1980.         list->field.bitpos = 8 * ms->c_value;
  1981.         list->field.bitsize = 0;
  1982.         nfields++;
  1983.         break;
  1984.  
  1985.       case C_FIELD:
  1986.  
  1987.         /* Get space to record the next field's data.  */
  1988.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1989.         new->next = list;
  1990.         list = new;
  1991.  
  1992.         /* Save the data.  */
  1993.         list->field.name = savestring (name, strlen (name));
  1994.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1995.         list->field.bitpos = ms->c_value;
  1996.         list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
  1997.         nfields++;
  1998.         break;
  1999.  
  2000.       case C_EOS:
  2001.         done = 1;
  2002.         break;
  2003.     }
  2004.     }
  2005.   /* Now create the vector of fields, and record how big it is.  */
  2006.  
  2007.   TYPE_NFIELDS (type) = nfields;
  2008.   TYPE_FIELDS (type) = (struct field *)
  2009.         obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
  2010.  
  2011.   /* Copy the saved-up fields into the field vector.  */
  2012.  
  2013.   for (n = nfields; list; list = list->next)
  2014.     TYPE_FIELD (type, --n) = list->field;
  2015.  
  2016.   return type;
  2017. }
  2018.  
  2019. /* Read a definition of an enumeration type,
  2020.    and create and return a suitable type object.
  2021.    Also defines the symbols that represent the values of the type.  */
  2022. /* Currently assumes it's sizeof (int) and doesn't use length.  */
  2023.  
  2024. /* ARGSUSED */
  2025. static struct type *
  2026. read_enum_type (index, length, lastsym)
  2027.      int index;
  2028.      int length;
  2029.      int lastsym;
  2030. {
  2031.   register struct symbol *sym;
  2032.   register struct type *type;
  2033.   int nsyms = 0;
  2034.   int done = 0;
  2035.   struct pending **symlist;
  2036.   struct coff_symbol member_sym;
  2037.   register struct coff_symbol *ms = &member_sym;
  2038.   struct internal_syment sub_sym;
  2039.   union internal_auxent sub_aux;
  2040.   struct pending *osyms, *syms;
  2041.   register int n;
  2042.   char *name;
  2043. #ifdef NAMES_HAVE_UNDERSCORE
  2044.   int offset = 1;
  2045. #else
  2046.   int offset = 0;
  2047. #endif
  2048.  
  2049.   type = coff_alloc_type (index);
  2050.   if (within_function)
  2051.     symlist = &local_symbols;
  2052.   else
  2053.     symlist = &file_symbols;
  2054.   osyms = *symlist;
  2055.  
  2056.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  2057.     {
  2058.       read_one_sym (ms, &sub_sym, &sub_aux);
  2059.       name = ms->c_name;
  2060.       name = (name[0] == '_' ? name + offset : name);
  2061.  
  2062.       switch (ms->c_sclass)
  2063.     {
  2064.       case C_MOE:
  2065.         sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  2066.         bzero (sym, sizeof (struct symbol));
  2067.  
  2068.         SYMBOL_NAME (sym) = savestring (name, strlen (name));
  2069.         SYMBOL_CLASS (sym) = LOC_CONST;
  2070.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  2071.         SYMBOL_VALUE (sym) = ms->c_value;
  2072.         add_symbol_to_list (sym, symlist);
  2073.         nsyms++;
  2074.         break;
  2075.  
  2076.       case C_EOS:
  2077.         /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
  2078.            up the count of how many symbols to read.  So stop
  2079.            on .eos.  */
  2080.         done = 1;
  2081.         break;
  2082.     }
  2083.     }
  2084.  
  2085.   /* Now fill in the fields of the type-structure.  */
  2086.  
  2087.   /* FIXME: Should be sizeof (int) on target, not host.  */
  2088.   TYPE_LENGTH (type) = sizeof (int);
  2089.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  2090.   TYPE_NFIELDS (type) = nsyms;
  2091.   TYPE_FIELDS (type) = (struct field *)
  2092.         obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
  2093.  
  2094.   /* Find the symbols for the values and put them into the type.
  2095.      The symbols can be found in the symlist that we put them on
  2096.      to cause them to be defined.  osyms contains the old value
  2097.      of that symlist; everything up to there was defined by us.  */
  2098.  
  2099.   for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
  2100.     {
  2101.       SYMBOL_TYPE (syms->symbol) = type;
  2102.       TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
  2103.       TYPE_FIELD_VALUE (type, n) = 0;
  2104.       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
  2105.       TYPE_FIELD_BITSIZE (type, n) = 0;
  2106.     }
  2107.   /* Is this Modula-2's BOOLEAN type?  Flag it as such if so. */
  2108.   if(TYPE_NFIELDS(type) == 2 &&
  2109.      ((!strcmp(TYPE_FIELD_NAME(type,0),"TRUE") &&
  2110.        !strcmp(TYPE_FIELD_NAME(type,1),"FALSE")) ||
  2111.       (!strcmp(TYPE_FIELD_NAME(type,1),"TRUE") &&
  2112.        !strcmp(TYPE_FIELD_NAME(type,0),"FALSE"))))
  2113.      TYPE_CODE(type) = TYPE_CODE_BOOL;
  2114.   return type;
  2115. }
  2116.  
  2117. /* Register our ability to parse symbols for coff BFD files */
  2118.  
  2119. static struct sym_fns coff_sym_fns =
  2120. {
  2121.     /* This assumes that 88kbcs implies TDESC and TDESC implies 88kbcs.
  2122.        If that's not true, this can be relaxed, but if it is true,
  2123.        it will just cause users grief if we try to read the wrong kind
  2124.        of symbol file.  */
  2125. #if defined (TDESC)
  2126.     "m88kbcs", 8,
  2127. #else /* not TDESC */
  2128.     "coff", 4,
  2129. #endif /* not TDESC */
  2130.     coff_new_init, coff_symfile_init, coff_symfile_read,
  2131. };
  2132.  
  2133. void
  2134. _initialize_coffread ()
  2135. {
  2136.   add_symtab_fns(&coff_sym_fns);
  2137. }
  2138.